// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./accountDetainedApplication.css");
	var databind = require("databind");
	var tools = require("tools");
	var financeId;
	var inventoryId = [];
	var remark;
	var tools = require("tools");

	var init = function(page) {
		financeId = page.params.financeId;
		inquireTallyingPageList();
		tools.inquireCommonSelectorList(9, $(".delayReson"));
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		//点击项目基本详情financeDetail
		$(".financeDetail").unbind("click").click(function(){
			router.route("account/accountDetail.html?financeId="+financeId,true);
                	
		});
		$("#detainedApplicationSubmitBtn").unbind("click").click(function(){
			inventoryId = [];
			remark = "";
			
			var inventorys = $("#accountDetainedApplication .detainedApplicationChecked:checked");
			$.each(inventorys, function() {
				inventoryId.push($(this).parents("tr").attr("inventoryId"));
			});
			
			remark = $.trim($("#detainedApplicationReason").val());
			
			if(inventoryId.length == 0){
				base.alert(2,"请选择要扣货的货箱!");
				
			}else if(remark==""){
				base.alert(2,"请填写扣货缘由！");
				
			}else{
				tools.protectDoubleClick();
				createDetainedApplication();
			}
		});
	};

	//查询理货任务列表
	var inquireTallyingPageList = function() {
		base.fnAjax({
			action: "inquireTallyingPageList.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#detainedTpl").html();
					var html = juicer(tpl, data);
					$("#detaniedBody").html(html);
					
					$(".detainedDetail").unbind("click").click(function(){
						router.route("account/accountTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
				} else {
				}
			}
		});
	};
	
	//扣货申请
	var createDetainedApplication = function() {
		var delayReson = $(".delayReson").val();
		base.fnAjax({
			action: "createDetainedApplication.json",
			async: false,
			oData: {
				financeId: financeId,
				remark: remark,
				inventoryId: inventoryId,
				applicantReson:delayReson
			}, //传递参数
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					router.route("account/accountDetail.html?financeId="+financeId);					
				} else {
					base.alert(3,data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});